﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class bookChapter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            showTheRightRegion(cmd);
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            //string strId = txtQryId.Text.Trim();
            //if (strId != "" && strId != null )
            //{
            //    strWhere += " and id=@id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            //} 
            string strBookid = txtQryBookid.Text.Trim();
            if (strBookid != null && strBookid != "" )
            {
                strWhere += " and bookid=@bookid";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@bookid", strBookid));
            } 
            string strChapterID = txtQryChapterID.Text.Trim();
            if (strChapterID != null && strChapterID != "" )
            {
                strWhere += " and chapterID=@chapterID";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@chapterID", strChapterID));
            } 
            //string strTitle = txtQryTitle.Text.Trim();
            //if (strTitle != "" && strTitle != null )
            //{
            //    strWhere += " and title=@title";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@title", strTitle));
            //} 
            //string strSummary = txtQrySummary.Text.Trim();
            //if (strSummary != "" && strSummary != null )
            //{
            //    strWhere += " and summary=@summary";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@summary", strSummary));
            //} 
            //string strBookContent = txtQryBookContent.Text.Trim();
            //if (strBookContent != "" && strBookContent != null )
            //{
            //    strWhere += " and bookContent=@bookContent";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@bookContent", strBookContent));
            //} 
            //string strAddDate = txtQryAddDate.Text.Trim();
            //if (strAddDate != "" && strAddDate != null )
            //{
            //    strWhere += " and addDate=@addDate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@addDate", strAddDate));
            //} 
            //string strUpdateDate = txtQryUpdateDate.Text.Trim();
            //if (strUpdateDate != "" && strUpdateDate != null )
            //{
            //    strWhere += " and updateDate=@updateDate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@updateDate", strUpdateDate));
            //} 
            //string strSortid = txtQrySortid.Text.Trim();
            //if (strSortid != "" && strSortid != null )
            //{
            //    strWhere += " and sortid=@sortid";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@sortid", strSortid));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 if (paraList.Count > 0)
                     litPromptMessage.Text = "没有查询到相关记录 ";
                 else
                 {
                     litPromptMessage.Text = " <a href='?cmd=add'>添加新纪录</a>";
                     divQuery.Visible = false;
                 }
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = false;
            int varId ;	//id
            int varBookid ;	//书编号
            int varChapterID ;	//章节序号
            string varTitle;	//标题
            string varSummary;	//摘要
            string varBookContent;	//内容
            DateTime varAddDate ;	//添加时间
            DateTime varUpdateDate ;	//最后修改时间
            int varSortid ;	//排序
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varBookid = int.Parse(ddlBookID.SelectedValue);
               varChapterID =int.Parse(txtChapterID.Text.Trim());	//章节序号
               varTitle =txtTitle.Text.Trim();	//标题
               varSummary =txtSummary.Text.Trim();	//摘要
               varBookContent =txtContent.Text.Trim();	//内容
               //varAddDate =DateTime.Parse(txtAddDate.Text.Trim());	//添加时间
               //varUpdateDate =DateTime.Parse(txtUpdateDate.Text.Trim());	//最后修改时间
               varSortid =int.Parse(txtSortid.Text.Trim());	//排序
               varExt1 = "";// txtExt1.Text.Trim();	//扩展1
               varExt2 = "";// txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId == 0) isAdd = true;
            #endregion

            #region 赋值model
            BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
            BZZ.Model.bookChapter model = new BZZ.Model.bookChapter();
            if (isAdd)
            {
                model.bookid = varBookid;	//书编号
                model.chapterID = varChapterID;	//章节序号
                model.title = varTitle;	//标题
                model.summary = varSummary;	//摘要
                model.bookContent = varBookContent;	//内容
                model.addDate = DateTime.Today;// varAddDate;	//添加时间
                model.updateDate = DateTime.Today;// varUpdateDate;	//最后修改时间
                model.sortid = varSortid;	//排序
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
                if (dal.Add(model) > 0)
                {
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
            else
            {
                model = dal.GetModel(varId);
                model.id = varId;	//id
                model.bookid = varBookid;	//书编号
                model.chapterID = varChapterID;	//章节序号
                model.title = varTitle;	//标题
                model.summary = varSummary;	//摘要
                model.bookContent = varBookContent;	//内容
                //model.addDate = varAddDate;	//添加时间
                model.updateDate = DateTime.Today;// varUpdateDate;	//最后修改时间
                model.sortid = varSortid;	//排序
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
				if (dal.Update(model) > 0)
                {
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
  
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                //litPromptMessage.Text = "操作失败";
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {
                bindBookID();
                //txtBookid.Text = "";
                txtChapterID.Text = "0";
                txtTitle.Text = "";
                txtSummary.Text = "";
                txtContent.Text = "";
                //txtAddDate.Text = "";
                //txtUpdateDate.Text = "";
                txtSortid.Text = "1";
                //txtExt1.Text = "";
                //txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                bindBookID();
                BZZ.Model.bookChapter model = new BZZ.Model.bookChapter();
                BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                //txtBookid.Text = model.bookid.ToString();
                ddlBookID.SelectedValue = model.bookid.ToString();
                txtChapterID.Text = model.chapterID.ToString();
                txtTitle.Text = model.title.ToString();
                txtSummary.Text = model.summary.ToString();
                txtContent.Text = model.bookContent.ToString();
                //txtAddDate.Text = model.addDate.ToString();
                //txtUpdateDate.Text = model.updateDate.ToString();
                txtSortid.Text = model.sortid.ToString();
                //txtExt1.Text = model.ext1.ToString();
                //txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                BZZ.Model.bookChapter model = new BZZ.Model.bookChapter();
                BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
                model = dal.GetModel(id);

                lblBookid.Text = model.bookid.ToString();
                lblChapterID.Text = model.chapterID.ToString();
                lblTitle.Text = model.title.ToString();
                lblSummary.Text = model.summary.ToString();
                lblBookContent.Text = model.bookContent.ToString();
                lblAddDate.Text = model.addDate.ToString();
                lblUpdateDate.Text = model.updateDate.ToString();
                lblSortid.Text = model.sortid.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion

        public void bindBookID()
        {
            string sql = "select id as datavalue,bookname as datatext from bzz_book order by id desc";
            BZZ.DAL.bookChapter dal = new BZZ.DAL.bookChapter();
            DataSet dset = dal.GetDatasetBySql(sql);
            ddlBookID.DataSource = dset;
            ddlBookID.DataBind();
            //ddlBookID.Items.Insert(0, new ListItem("", ""));
        }

        public string showBookName(object oBookid)
        {
            int id = Convert.ToInt32(oBookid);
            BZZ.DAL.book dal = new BZZ.DAL.book();
            BZZ.Model.book mod = dal.GetModel(id);
            string bookName = mod.bookName;
            return bookName;
        }

    }


}

